package service;


import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;

public class CookieDemo extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        PrintWriter out = resp.getWriter();

        Cookie[] cookies = req.getCookies();
        if (cookies == null) {
            out.write("First");
        } else {
            out.write("上次时间是：");
            for (int i = 0; i < cookies.length; i++) {
                if (cookies[i].getName().equals("lastTime")) {
                    long l = Long.parseLong(cookies[i].getValue());
                    String s = new Date(l).toString();
                    out.write(s);
                }

            }
        }
        resp.addCookie(new Cookie("lastTime", System.currentTimeMillis()+""));

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPost(req, resp);
    }
}
